import InteractiveBase from "../../../LevelCommon/InteractiveBase"
import PasswordText from "./PasswordText";
import LevelBaseControl from "../../../LevelCommon/LevelBaseControl";
import { utils } from "../../../../utils/CommonUtil";
import Sound from "../../../../constant/SoundNameConstant";
import DialogDataConst from "../../../../constant/DialogDataConst";

export default class PasswordLock extends InteractiveBase{
    /** 正确密码*/
    public  passWord:string;
    private  passWordLength:number;
    /** 密码text*/
    private passWordText:PasswordText;
    /** 是否已经打开锁*/
    private isOpenLock:boolean = false;
    private curLevelControl:LevelBaseControl;

    onStart()
    {
        this.InitComponent();
        this.SetAllSonObj();
        this.curLevelControl = window.gameFacade.LevelMng.GetCurLevelControl();
    }
    private InitComponent()
    {
        this.passWordLength = this.passWord.length;
        let childPassWordText = this.owner.getChildByName("PasswordText") as Laya.Sprite3D;
        childPassWordText.active = true;
        this.passWordText = utils.CommonUtils.CustomAddComponent(childPassWordText,PasswordText) as PasswordText;
        this.passWordText.Init();
    }
    protected InteractiveLogic():boolean
    {
        return false;
    }
    /** 按下按钮*/
    public  InputPassKey( passValue:string):void
    {
        /** 锁已经打开了 就不用再输入了*/
        if (this.isOpenLock)
        {
            return
        };

        /** 先判断是不是数字*/
        if (utils.CommonUtils.isNumber(passValue))
        {
            /** 是数字 就输入*/
            let curPassWord:string = this.passWordText.Text;
            /** 如果当前密码位数已经最大了 就不操作了*/
            if (curPassWord.length == this.passWordLength)
            {
                return;
            }

            this.passWordText.SetPasswordTexture(curPassWord + passValue);
        }
        else
        {
            switch (passValue)
            {
                /** 回车  检查密码对不对*/
                case "enter":
                    this.CheckPassWord();
                    break;
                default:
                    break;
            }
        }        
    }
    /** 检测密码对错*/
    private CheckPassWord()
    {
        let curPassWord:string = this.passWordText.Text;
        /** 密码输完了 检测对错*/
        if (curPassWord.length == this.passWordLength && curPassWord == this.passWord)
        {           
            /** 密码输入正确*/
            console.log("密码输入正确！！！");
            /** 在关卡逻辑里 执行开锁操作*/
            this.CallToLevel();
            this.isOpenLock = true;           
        }
        else
        {
            window.gameFacade.SoundMng.playSound(Sound.soundClipArr.ERROR);
            this.curLevelControl.ShowDialog(DialogDataConst.password_fault);
            /** 密码错误*/
            this.ResetPassWord();
        }
    }
    /** 密码归零*/
    private ResetPassWord()
    {
        this.passWordText.ClearPasswordTexture();
    }
}